import { Component, Injectable, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { LocalStorage } from '@core/local.storage';
import { StamonthplanService } from './statmonthplan.service';

@Injectable({ providedIn: 'root' })

@Component({
  selector: 'app-statmonthplan',
  templateUrl: './statmonthplan.component.html',
  styleUrls: ['./statmonthplan.component.less']
})
export class StatmonthplanComponent implements OnInit {
  queryData_gq: any;
  queryData_cj: any;
  queryData_dd: any;
  tabIndex = 0;
  page2Param: any = {};
  userInfo = JSON.parse(this.ls.get('loginInfo'));
  userType = '';
  ddType: any;
  ddTypeName: any = {
    gcs: '段月计划',
    jskz: '段月计划',
    dz: '段月计划'
  };
  cjList: any = [];
  showCjlist = true;
  isTj = '';

  constructor(
    private activatedRoute: ActivatedRoute,
    private stamonthplanService: StamonthplanService,
    private ls: LocalStorage
  ) { }

  ngOnInit() {
    this.activatedRoute.params.subscribe(params => {
      if (params && params.type) {
        this.ddType = params.type;
      }
    });
    this.initUserType();
  }

  initUserType() {
    switch (this.userInfo.N_LEVEL) {
      case 10:
        this.userType = 'gq';
        this.queryData_gq = { orgtype: 'gq' };
        break;
      case 30:
        this.userType = 'cj';
        this.queryData_cj = { orgtype: 'cj' };
        this.queryData_gq = { orgtype: 'gq' };
        break;
      case 50:
        this.userType = 'dd';
        this.queryData_dd = { orgtype: 'dd', };
        this.queryData_cj = { orgtype: 'cj' };
        this.queryData_gq = { orgtype: 'gq' };
        this.getCj();
        break;
    }
  }

  getCj() {
    this.stamonthplanService.getCjList().subscribe(res => {
      this.cjList = res;
    })
  }

  cjDbclick(row) {
    this.showGq(row);
  }

  showCj() {
    this.showCjlist = true;
  }
  showGq(row) {
    this.showCjlist = false;
    this.queryData_gq.cjorg = row.opValue;
  }

  tabSelectChange(e) {
    const tabCfg = {
      cj: [this.queryData_cj, this.queryData_gq],
      dd: [this.queryData_dd, this.queryData_cj, this.queryData_gq],
    };
    const paramCfg = {
      cj: ['cj', 'gq'],
      dd: ['dd', 'cj', 'gq'],
    }
    this[`queryData_${paramCfg[this.userType][e.index]}`] = JSON.parse(JSON.stringify(tabCfg[this.userType][e.index]));
  }
}
